﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TouristAgencyDataLayer
{
    public class MeansOfTransports
    {
        public static void CreateHotelMeanOfTransport(string mean)
        {
            if (!string.IsNullOrEmpty(mean))
            {
                TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

                MeansOfTransport m = new MeansOfTransport()
                {
                    MeanOFTransport = mean
                };

                context.MeansOfTransports.InsertOnSubmit(m);
                context.SubmitChanges();
            }
        }

        public static void UpdateMeanOfTransport(int meanId, string mean)
        {
            if (!string.IsNullOrEmpty(mean))
            {
                TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

                var res =
                    (from b in context.MeansOfTransports
                     where b.MeanOfTransportId == meanId
                     select b).FirstOrDefault();

                if (res != null)
                {
                    res.MeanOFTransport = mean;
                    context.SubmitChanges();
                }
            }
        }

        public static void DeleteMeanOfTransport(int meanId)
        {

            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            var mean =
                (from b in context.MeansOfTransports
                 where b.MeanOfTransportId == meanId
                 select b).FirstOrDefault();

            if (mean != null)
            {
                var tours =
                    from t in context.Tours
                    where t.MeanOfTransportId == meanId
                    select t;

                foreach (var t in tours)
                {
                    Tours.UpdateTour(t.TourId, t.DestinationId, t.StartDate, t.EndDate, null, t.BoardId, t.Description, t.Price);
                }

                context.MeansOfTransports.DeleteOnSubmit(mean);
                context.SubmitChanges();
            }
        }

        public static IQueryable<MeansOfTransport> GetMeansOfTransport()
        {
            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            return context.MeansOfTransports;
        }

        public static MeansOfTransport GetMeanOfTransportById(int? id)
        {
            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            var res =
                (from m in context.MeansOfTransports
                 where m.MeanOfTransportId == id
                 select m).FirstOrDefault();

            if (res != null)
            {
                return res;
            }

            return null;
        }
    }
}
